#!/bin/bash

# This script downloads the index pages of the public printers, then parses their output for the toner level

# Create the array for printer names

printer_names=( baskerville cleveland lib_iif lib_1st_flr lib_2nd_flr norris2 read2 \
		scilab scilib jrc_email younker2 helpdesk2 duerer escher gencomp2 inslab2 ccl3_bw )

for ((i=0; i < ${#printer_names[@]}; i++))
do

# Download pages with wget

wget -T 2 -t 2 "${printer_names[$i]}".grinnell.edu -O $HOME/printers/status_pages/"${printer_names[$i]}"

# Now, parse the files and write contents to the corresponding toner level file

grep Cartridge $HOME/printers/status_pages/"${printer_names[$i]}" | egrep "[0-9]{1,3}" -o > $HOME/printers/toner_level/"${printer_names[$i]}"_level
done

# Rose is strange, so we have to account for the lack of a certificate check...

wget -T 2 -t 2 --no-check-certificate rose.grinnell.edu -O $HOME/printers/status_pages/rose

# We also have to take account of the fact that ESCHER is a color printer, and the fact that we have to parse the status pages for
# HELPDESK, DUERER, and ROSE in a different manner

grep Black $HOME/printers/status_pages/escher | egrep "[0-9]{1,3}" -o > $HOME/printers/toner_level/escher_black_level
grep Cyan $HOME/printers/status_pages/escher | egrep "[0-9]{1,3}" -o > $HOME/printers/toner_level/escher_cyan_level
grep Magenta $HOME/printers/status_pages/escher | egrep "[0-9]{1,3}" -o > $HOME/printers/toner_level/escher_magenta_level
grep Yellow $HOME/printers/status_pages/escher | egrep "[0-9]{1,3}" -o > $HOME/printers/toner_level/escher_yellow_level
grep Cartridge $HOME/printers/status_pages/helpdesk2 | awk '{print $6}' | egrep "[0-9]{1,3}" -o > $HOME/printers/toner_level/helpdesk2_level
grep Cartridge $HOME/printers/status_pages/duerer | awk '{print $6}' | egrep "[0-9]{1,3}" -o > $HOME/printers/toner_level/duerer_level
grep Cartridge $HOME/printers/status_pages/rose | awk '{print $6}' | egrep "[0-9]{1,3}" -o > $HOME/printers/toner_level/rose_level

exit 0
